package com.github.easyms.server.controller.auth;

import com.github.easyms.common.base.BaseController;
import com.github.easyms.common.entity.auth.Role;
import com.github.easyms.common.entity.auth.User;
import com.github.easyms.common.util.EncryptionHelper;
import com.github.easyms.common.util.ValidatorHelper;
import com.github.easyms.common.vo.PageParams;
import com.github.easyms.common.vo.ResultPage;
import com.github.easyms.common.vo.auth.ResetPasswordVo;
import com.github.easyms.common.vo.auth.UserVo;
import com.github.easyms.server.service.auth.RoleService;
import com.github.easyms.server.service.auth.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户管理接口
 *
 * @author Leo.Liao
 * @date 2020-04-11 12:14:00
 */
@Api(tags = "用户管理接口")
@RestController
@RequestMapping("/api/auth/user")
public class UserController extends BaseController<User> {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;

    @Override
    public ResponseEntity<ResultPage<User>> list(PageParams pageParams, User conditions) {
        service = userService;
        return super.list(pageParams, conditions);
    }

    @Override
    public ResponseEntity<List<User>> listCombobox(User entity,String q) {
        service = userService;
        return super.listCombobox(entity,q);
    }

    @Override
    public ResponseEntity<User> insert(@RequestBody User entity) {
        service = userService;
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        entity.setEditUser(currentUser.getUserId());
        return super.insert(entity);
    }

    @Override
    public ResponseEntity<User> update(@RequestBody User entity) {
        service = userService;
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        entity.setEditUser(currentUser.getUserId());
        return super.update(entity);
    }

    @Override
    public ResponseEntity<User> delete(@RequestBody User entity) {
        service = userService;
        return super.delete(entity);
    }

    /**
     * 修改密码
     * @param resetPasswordVo
     * @return
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/changePassword")
    public ResponseEntity<Boolean> changePassword(@RequestBody ResetPasswordVo resetPasswordVo){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        resetPasswordVo.setUserId(currentUser.getUserId());
        ValidatorHelper.validateEntity(resetPasswordVo);
        userService.changePassword(resetPasswordVo);
        return ResponseEntity.ok(true);
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @ApiOperation(value = "修改密码")
    @PostMapping("/resetPassword")
    public ResponseEntity<Boolean> resetPassword(String userId){
        userService.resetPassword(userId);
        return ResponseEntity.ok(true);
    }

    /**
     * 密码校验
     * @param password
     * @return
     */
    @ApiOperation(value = "密码校验")
    @PostMapping("/checkPassword")
    public ResponseEntity<Boolean> checkPassword(String password){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        password = EncryptionHelper.md5Encrypt(password, currentUser.getSalt());
        return ResponseEntity.ok(password.equals(currentUser.getPassword()));
    }

    /**
     * 账号校验
     * @param account
     * @return
     */
    @ApiOperation(value = "账号校验")
    @PostMapping("/checkAccount")
    public ResponseEntity<Boolean> checkAccount(String userId,String account){
        return ResponseEntity.ok(userService.checkAccount(userId,account));
    }

    /**
     * 获取当前登陆用户信息
     * @return
     */
    @ApiOperation(value = "获取当前登陆用户信息")
    @GetMapping("/getCurrentUser")
    public ResponseEntity<UserVo> getCurrentUserName(){
        UserVo userVo = new UserVo();
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        Role currentRole = roleService.getById(currentUser.getRoleId());
        BeanUtils.copyProperties(currentUser,userVo);
        userVo.setRoleName(currentRole.getRoleName());
        return ResponseEntity.ok(userVo);
    }

    /**
     * 注销登陆，清除Token
     * @return
     */
    @ApiOperation(value = "注销登陆，清除Token")
    @PostMapping("/logout")
    public ResponseEntity<Boolean> logout(){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        return ResponseEntity.ok(userService.deleteToken(currentUser.getUserId()));
    }

    /**
     * 更新语言
     * @param language
     * @return
     */
    @ApiOperation(value = "更新用户语言")
    @PostMapping("/updateUserLanguage")
    public ResponseEntity<Boolean> updateUserLanguage(String language){
        User currentUser = (User) SecurityUtils.getSubject().getPrincipal();
        currentUser.setLanguage(language);
        return ResponseEntity.ok(userService.updateById(currentUser));
    }

}
